package cn.zhaoyuening.zshop4j.content.service.impl;


import cn.zhaoyuening.zshop4j.common.bean.EasyUiListResult;
import cn.zhaoyuening.zshop4j.common.bean.EasyUiTreeNode;
import cn.zhaoyuening.zshop4j.common.bean.ZShop4jResponceResult;
import cn.zhaoyuening.zshop4j.common.utils.JsonUtils;
import cn.zhaoyuening.zshop4j.content.service.ContentService;
import cn.zhaoyuening.zshop4j.content.service.utils.jedis.JedisClient;
import cn.zhaoyuening.zshop4j.manager.mapper.TbContentMapper;
import cn.zhaoyuening.zshop4j.manager.pojo.TbContent;
import cn.zhaoyuening.zshop4j.manager.pojo.TbContentCategory;
import cn.zhaoyuening.zshop4j.manager.pojo.TbContentExample;
import cn.zhaoyuening.zshop4j.manager.pojo.TbItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by Zhao on 2017/4/15.
 */
@Service("contentService")
public class ContentServiceImpl implements ContentService{
    private final String HASH_KEY_INDEX_CONTENT = "INDEX_CONTENT";
    private static Logger logger = Logger.getLogger(ContentServiceImpl.class.getName());
    @Autowired
    private TbContentMapper contentMapper;
    @Autowired
    private JedisClient jedisClient;

    @Override
    public EasyUiListResult<TbContent> findByCategoryId(long contentCategoryId,int pageNum,int pageSize) {
        EasyUiListResult<TbContent> easyUiListResult = new EasyUiListResult<>();

        PageHelper.startPage(pageNum,pageSize);

        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(contentCategoryId);
        List<TbContent> list = contentMapper.selectByExample(example);

        PageInfo<TbContent> pageInfo = new PageInfo<TbContent>(list);

        easyUiListResult.setRows(pageInfo.getList());
        easyUiListResult.setTotal(pageInfo.getTotal());

        return easyUiListResult;
    }

    @Override
    public List<TbContent> findByCategoryId(long contentCategoryId) {
        try {
            // 从redis 获取缓存数据
            String data = jedisClient.hget(HASH_KEY_INDEX_CONTENT, contentCategoryId + "");
            if (!StringUtils.isEmpty(data)){
                //获取成功直接返回
                List<TbContent> resultList = JsonUtils.jsonToList(data, TbContent.class);
                logger.info("从redis获取HASH key为"+HASH_KEY_INDEX_CONTENT+" field为 "+contentCategoryId+" 成功！" );
                return resultList;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //没有从缓存中查询到数据 通过数据库查询
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(contentCategoryId);
        List<TbContent> list = contentMapper.selectByExample(example);
        try {
            //以json数据保存到redis中
            jedisClient.hset(HASH_KEY_INDEX_CONTENT, contentCategoryId + "", JsonUtils.objectToJson(list));
            logger.info("从redis保存HASH key为"+HASH_KEY_INDEX_CONTENT+" field为 "+contentCategoryId+" 成功！" );
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public ZShop4jResponceResult insert(TbContent content) {
        int insertResult = contentMapper.insertSelective(content);
        if (insertResult > 0) {
            //缓存同步
            jedisClient.hdel(HASH_KEY_INDEX_CONTENT, content.getCategoryId().toString());
            return ZShop4jResponceResult.ok();
        }
        return  ZShop4jResponceResult.fail();
    }

    @Override
    public ZShop4jResponceResult deleteByIdList(Long[] ids,Long categoryId) {
        //查询该
        List<Long> idList = Arrays.asList(ids);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idList);
        int deleteResult = contentMapper.deleteByExample(example);
        if (deleteResult > 0) {
            //缓存同步
            jedisClient.hdel(HASH_KEY_INDEX_CONTENT, categoryId.toString());
            return ZShop4jResponceResult.ok();
        }
        return  ZShop4jResponceResult.fail();
    }

    @Override
    public TbContent findById(long id) {
        TbContent tbContent = contentMapper.selectByPrimaryKey(id);
        return tbContent;
    }

    @Override
    public ZShop4jResponceResult updateContent(TbContent content) {
        int updateResult = contentMapper.updateByPrimaryKeySelective(content);
        if (updateResult > 0) {
            //缓存同步
            jedisClient.hdel(HASH_KEY_INDEX_CONTENT, content.getCategoryId().toString());
            return ZShop4jResponceResult.ok();
        }
        return  ZShop4jResponceResult.fail();
    }
}
